package com.xwj.service.Impl;

import com.xwj.dao.RaStatusInfoDao;
import com.xwj.entity.RaStatusInfo;
import com.xwj.service.Interface.RaStatusInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by xuweijie on 2018/1/19.
 */
@Slf4j
@Service
@CacheConfig(cacheNames = "RaStatusInfoCache")
public class RaStatusInfoServiceImpl implements RaStatusInfoService {

    @Autowired
    private RaStatusInfoDao raStatusInfoDao;

    @Override
    @Cacheable(key = "#root.methodName", unless = "#result.size() == 0")
    public List<RaStatusInfo> findAllStatusInfo() {
        try {
            return raStatusInfoDao.findAllStatusInfo();
        } catch (Exception e) {
            log.error("RaStatusInfoService异常:", e);
            return Collections.emptyList();
        }
    }

    @Override
    @Cacheable(key = "'status_'+#label", unless = "#result.size() == 0")
    public List<RaStatusInfo> findStatusByLabel(String label) {
        try {
            return raStatusInfoDao.findStatusByLabel(label);
        } catch (Exception e) {
            log.error("RaStatusInfoService异常:", e);
            return Collections.emptyList();
        }
    }

    @Override
    @Cacheable(key = "'status_'+ #map.get('label') +'_'+ #map.get('type')", unless = "#result == ''")
    public String findOneStatus(Map<String, Object> map) {
        try {
            return raStatusInfoDao.findOneStatus(map);
        } catch (Exception e) {
            log.error("RaStatusInfoService异常:", e);
            return StringUtils.EMPTY;
        }
    }
}
